﻿using PhotoGallery.Data;
using PhotoGallery.Data.Entity;
using PhotoGallery.Data.Repository;
using PhotoGallery.Infrastructure;
using PhotoGallery.Models.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace PhotoGallery.Models 
{
    public class PublicAlbumInfo
    {
        public int ID { get; set; }
        [StringLength(25, ErrorMessageResourceType = typeof(ErrorMessages),
            ErrorMessageResourceName = "nameIsTooLong")]
        [LocalizedDisplayName("title", NameResourceType = typeof(Names))]
        public string Title { get; set; }
        [LocalizedDisplayName("folder", NameResourceType = typeof(Names))]
        public string FolderName { get; set; }
        public string FileName { get; set; }
        [DataType(DataType.Date)]
        [LocalizedDisplayName("date", NameResourceType = typeof(Names))]
        public DateTime? CreateDate { get; set; }
        public string Url { get; set; }
        public string Owner { get; set; }
        public string FullFileName { get; set; }
        public SharedAlbum Shared { get; set; }

        public PublicAlbumInfo()
        {

        }

        public PublicAlbumInfo(int id, string title, string folderName, string fileName, DateTime createdDate, string url, string owner, SharedAlbum shared, string fullFileName)
        {
            ID = id;
            Title = title;
            FolderName = folderName;
            FileName = fileName;
            CreateDate = createdDate;
            //Path = path;
            Url = url;
            Owner = owner;
            Shared = shared;
            FullFileName = fullFileName;
        }
    }

    public class PublicAlbumsModel : BaseModel
    {
        string path, url;
        PublicAlbumRepository repository ;

        public PublicAlbumsModel()
        {
            path = BasePath + Settings.UploadsFolder;
            url = Settings.UploadsUrl;
            repository = new PublicAlbumRepository();
        }

        public IEnumerable<PublicAlbumInfo> Get()
        {
            List<PublicAlbumInfo> albums = new List<PublicAlbumInfo>();
            var data = repository.Get(System.Web.HttpContext.Current.User.Identity.IsAuthenticated);
            foreach (var entity in data)
            {
                var item = new PublicAlbumInfo()
                {
                    ID = entity.ID,
                    Title = entity.Title,
                    FolderName = entity.FolderName,
                    FileName = entity.FileName,
                    CreateDate = entity.CreateDate,
                    Shared = entity.Shared,
                    Owner = entity.Owner,

                    Url = url + entity.Owner + "/" + entity.FileName,
                    FullFileName = path + entity.Owner + "\\" + entity.FileName
                };
                albums.Add(item);
            }
            return albums;
        }

        public PublicAlbumInfo Get(int id)
        {
            var entity = repository.Get(id);
            var item = new PublicAlbumInfo()
            {
                ID = entity.ID,
                Title = entity.Title,
                FolderName = entity.FolderName,
                FileName = entity.FileName,
                CreateDate = entity.CreateDate,
                Shared = entity.Shared,
                Owner = entity.Owner,

                Url = url + entity.Owner + "/" + entity.FileName,
                FullFileName = path + entity.Owner + "\\" + entity.FileName
            };

            return item;
        }

        //public int Create(PublicAlbum album, HttpPostedFileBase file)
        //{
        //    return repository.Create(album, file);
        //}

        //public void Update(PublicAlbum album, HttpPostedFileBase file)
        //{
        //    repository.Update(album, file);
        //}

        //public void Delete(int id)
        //{
        //    repository.Delete(id);
        //}
    }
}